package com.beadhouse.customer.support.service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;


import com.beadhouse.common.exeption.BeadHouseException;
import com.beadhouse.customer.ao.CustomerAo;
import com.beadhouse.customer.dao.CustomerDao;
import com.beadhouse.customer.service.CustomerService;
import com.beadhouse.entity.CustomerEntity;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class CustomerServiceImpl implements CustomerService{
	final CustomerDao cdao;
	
	public CustomerServiceImpl(CustomerDao cdao) {
		this.cdao=cdao;
	}
	@Override
	public void addCustomer(CustomerAo ao) throws Exception {
		String customerAge=ao.getCustomerAge();	
		Boolean customerSex;
		String idCard=ao.getIdCard();
		String roomNumber=ao.getRoomNumber();//房间号可暂时不填
		String elderType=ao.getElderType();	 //老人类型暂时不用
		DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");//网上百度的String转化成时间Date
		DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd"); 
		
		//合同到期时间
		String contactTel=ao.getContactTel();//联系电话
		String remark=ao.getRemark();//备注可不填
		String customerName=ao.getCustomerName();//必填
		
		
		if(ao.getRecordId()==null) {
			throw new BeadHouseException("档案号不能为空");
			
		}
		String record=ao.getRecordId();
		int recordId=Integer.parseInt(record);
		
		if(customerAge==null) {
			throw new BeadHouseException("年龄不能为空");
			
		}
		int age=Integer.parseInt(customerAge);	
		
		 if("男".equals(ao.getCustomerSex()))
    	 {
    	   customerSex=true;
    	   }
       else {
    	  customerSex=false;
         }
		
		 if(idCard==null) {
				throw new BeadHouseException("身份证号不能为空");
				
			}
	
		 if(customerName==null) { 
				throw new BeadHouseException("姓名不能为空");
				
			}
		
		 if(ao.getCheckinDate()==null) {
				throw new BeadHouseException("入住时间不能为空");
				
			}
		 Date checkinDate=format1.parse(ao.getCheckinDate());//入住时间	
		 if(ao.getExpirationDate()==null) {
				throw new BeadHouseException("合同到期时间不能为空");
				
			}
		 Date expirationDate=format1.parse(ao.getExpirationDate());			
		CustomerEntity u=new CustomerEntity();
		
		u.setCustomerName(customerName);
		u.setCustomerAge(age);
		u.setCustomerSex(customerSex);
		u.setRecordId(recordId);
		u.setRoomNumber(roomNumber);
        u.setIdcard(idCard);
		u.setElderType(elderType);
		u.setCheckinDate(checkinDate);
		u.setExpirationDate(expirationDate);
		u.setContactTel(contactTel);
		u.setRemark(remark);
		cdao.insert(u);
		
	}

	@Override
	public void updateCustomer(CustomerAo ao) throws Exception {
		if(ao.getId()==null) {
			throw new BeadHouseException("客户id不能为空");
		}
		int id=Integer.parseInt(ao.getId());
        Map<String,Object> map=new HashMap<>();
        if(ao.getCustomerName()!=null)
        {
        	map.put("customerName", ao.getCustomerName());}
        if(ao.getCustomerAge()!=null)
        {
        	map.put("customerAge", ao.getCustomerAge());
        }
        if(ao.getCustomerSex()!=null)
        {    
           if("男".equals(ao.getCustomerSex()))
        	 {
        	   map.put("customerSex", true);
        	   }
           else {
        	   map.put("customerSex", false);
           }
        }   
        if(ao.getIdCard()!=null)
        {
        	map.put("idCard",ao.getIdCard());
        }
        if(ao.getRoomNumber()!=null)
        {
        	map.put("roomNumber",ao.getRoomNumber());
        }
        if(ao.getRecordId()!=null)
        {
        map.put("recordId", ao.getRecordId());
        }
        if(ao.getElderType()!=null)
        {map.put("elderType", ao.getElderType());}
        if(ao.getCheckinDate()!=null)
        { map.put("checkinDate", ao.getCheckinDate());}
        if(ao.getExpirationDate()!=null)
        {map.put("expirationDate", ao.getExpirationDate());}
        if(ao.getContactTel()!=null)
        { map.put("contactTel", ao.getContactTel());}
        if(ao.getRemark()!=null)
        {  map.put("remark", ao.getRemark());
        
        }
        if(ao.getUserId()!=null)
        {  
        	map.put("userId", Integer.parseInt(ao.getUserId()));
        
        }
        map.put("updateTime", new Date());
        cdao.update(id, map);
		
	}

	@Override
	public void retreatCustomer(CustomerAo ao) throws Exception {
		if(ao.getId()==null) {
			throw new BeadHouseException("办理退住顾客id不能玩为空");
		}
        int id=Integer.parseInt(ao.getId());
        Map<String, Object> map=new HashMap<>();
        map.put("isDeleted", true);
		cdao.update(id, map);
	}

	@Override
	public CustomerEntity selectCutomerOne(CustomerAo ao) throws Exception {
		int id;
		int idCard;
		Map<String, Object> map=new HashMap<>();	
		if((ao.getId()==null)&&(ao.getIdCard()==null)) {
			throw new BeadHouseException("客户标识不能为空");
		}
		if((ao.getId()!=null)&&(ao.getIdCard()!=null)) {
			throw new BeadHouseException("客户标识必须唯一");
		}
		if((ao.getId()!=null)&&(ao.getIdCard()==null)) {
			 id=Integer.parseInt(ao.getId());
			 map.put("id", id);
		}
		if((ao.getId()==null)&&(ao.getIdCard()!=null)) {
		
			idCard=Integer.parseInt(ao.getIdCard());
			map.put("idCard",idCard);	
		}		
	    	
		CustomerEntity customer=cdao.selectOne(map);
		return customer;
	}

	

	@Override
	public List<CustomerEntity>  selectCustomerList(CustomerAo ao) throws Exception {
		
		
		String key=null;
		if(ao.getKey()!=null) {
		  key="%"+ao.getKey()+"%";
		}
		
		List<CustomerEntity> list=cdao.selectList(key);
		
		return list;
	}
	public PageInfo<CustomerEntity>  selectCustomerList1(CustomerAo ao,String pageNo) throws Exception {
		int pageNumber;
		int pageSize=3;
		if (pageNo==null)
		{
			pageNumber=1;
		}
		else
		{
			pageNumber=Integer.parseInt(pageNo);
		}
		String key=null;
		if(ao.getKey()!=null) {
		  key="%"+ao.getKey()+"%";
		}
		PageHelper.startPage(pageNumber, pageSize);
		List<CustomerEntity> list=cdao.selectList(key);
		PageInfo<CustomerEntity> pageInfo=new PageInfo<>(list);
		return pageInfo;
	}
	@Override
	public void deleteCustomer(CustomerAo ao) throws Exception {
		int id=Integer.parseInt(ao.getId());
		
		CustomerEntity s=new CustomerEntity();
		s.setId(id);
		cdao.delete(id);

		
	}



}
